package cn.tianyigps.tianyigpsapiv2.controller;

import cn.tianyigps.tianyigpsapiv2.exception.PermissionDeniedException;
import cn.tianyigps.tianyigpsapiv2.param.enums.ResponseEnum;
import cn.tianyigps.tianyigpsapiv2.param.vo.ResponseVo;
import cn.tianyigps.tianyigpsapiv2.service.EquipmentConfigService;
import cn.tianyigps.tianyigpsapiv2.service.EquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2019/4/26_16:57
 */
@Slf4j
@RestController
@RequestMapping("/equipment/config")
@CrossOrigin
@Api(value = "EquipmentConfigController", description = "设备配置接口")
public class EquipmentConfigController {

    @Autowired
    private EquipmentConfigService equipmentConfigService;

    /**
     * 设备配置设置
     * @param equipmentId
     * @param map {"key":"","value":""}
     * @return
     */
    @PostMapping("/{equipmentId}/set")
    @ApiOperation(value = "设备配置设置", notes = "测试接口")
    public ResponseVo set(@PathVariable String equipmentId, @RequestBody Map map){
        log.info("设备配置设置，equipmentId==>{}，key,value==>{}",equipmentId,map);
        try {
            String key = (String) map.get("key");
            String value = (String) map.get("value");
            equipmentConfigService.set(equipmentId,key,value);
        } catch (PermissionDeniedException e) {
            log.error(e.getMessage(),e);
            return ResponseVo.response(ResponseEnum.PERMISSION_DENIED);
        }
        return ResponseVo.success();
    }

    @GetMapping("/{equipmentId}/get/{key}")
    @ApiOperation(value = "设备配置设置查询", notes = "测试接口")
    public ResponseVo get(@PathVariable String equipmentId, @PathVariable String key){
        log.info("设备配置查询，equipmentId==>{}，key==>{}",equipmentId,key);
        String value = equipmentConfigService.get(equipmentId,key);
        if(StringUtils.isEmpty(value)){
            return ResponseVo.notFoundRequest("没找到此项配置！");
        }
        return ResponseVo.response(ResponseEnum.QUERY_SUCCESS,value);
    }

    @DeleteMapping("/{equipmentId}/del/{key}")
    @ApiOperation(value = "设备配置设置删除", notes = "测试接口")
    public ResponseVo del(@PathVariable String equipmentId, @PathVariable String key){
        log.info("设备配置删除，equipmentId==>{}，key==>{}",equipmentId,key);
        boolean success = equipmentConfigService.del(equipmentId,key);
        if(!success){
            return ResponseVo.response(ResponseEnum.DELETE_FAILURE);
        }
        return ResponseVo.response(ResponseEnum.DELETE_SUCCESS);
    }

    @GetMapping("/{equipmentId}/list")
    @ApiOperation(value = "设备配置设置列表", notes = "测试接口")
    public ResponseVo list(@PathVariable String equipmentId){
        log.info("设备配置列表，equipmentId==>{}",equipmentId);
        Map map = equipmentConfigService.list(equipmentId);
        if(CollectionUtils.isEmpty(map)){
            return ResponseVo.response(ResponseEnum.QUERY_NULL);
        }
        return ResponseVo.response(ResponseEnum.QUERY_SUCCESS,map);
    }
}
